﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-14>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：银行批量代理业务
//********************************************************************************//
using System;
using System.Web.UI;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;
using System.Web.UI.WebControls;
using System.Web;
using System.Text;

namespace Web.Pages.YongShui
{
    public partial class YinHangPLDLYW : BasePage
    {
        #region 属性

        private string zhandianbh
        {
            get
            {
                return Convert.ToString(ViewState["zhandianbh"]);
            }
            set
            {
                ViewState["zhandianbh"] = value;
            }
        }

        /// <summary>
        /// 记录付款人合同号列表结果集
        /// </summary>
        private DataTable dtSourceFUKUANRHTH
        {
            get
            {
                return ViewState["dtSourceFUKUANRHTH"] as DataTable;
            }
            set
            {
                ViewState["dtSourceFUKUANRHTH"] = value;
            }
        }

        /// <summary>
        /// 记录银行批量代理汇总列表1结果集
        /// </summary>
        private DataTable dtSourceYINHANGPLDLHZ1
        {
            get
            {
                return ViewState["dtSourceYINHANGPLDLHZ1"] as DataTable;
            }
            set
            {
                ViewState["dtSourceYINHANGPLDLHZ1"] = value;
            }
        }

        /// <summary>
        /// 记录银行批量代理汇总列表2结果集
        /// </summary>
        private DataTable dtSourceYINHANGPLDLHZ2
        {
            get
            {
                return ViewState["dtSourceYINHANGPLDLHZ2"] as DataTable;
            }
            set
            {
                ViewState["dtSourceYINHANGPLDLHZ2"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 托收明细BLL
        /// </summary>
        YS_SF_TUOSHOUMXBLL _YS_SF_TUOSHOUMXBLL = new YS_SF_TUOSHOUMXBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                LoadDataFUKUANRHTH(true);
                LoadDataYINHANGPLDLHTL1(true);
                LoadDataYINHANGPLDLHTL2(true);
            }
            this.ScriptManager1.RegisterPostBackControl(btnExportExcel);
            LoadDataFUKUANRHTH(false);
            LoadDataYINHANGPLDLHTL1(false);
            LoadDataYINHANGPLDLHTL2(false);
        }

        /// <summary>
        /// 年月文本框回车事件
        /// </summary>
        protected void btnconSearch_Click(object sender, EventArgs e)
        {
            LoadDataFUKUANRHTH(true);
        }

        /// <summary>
        /// 生成文件按钮
        /// </summary>
        protected void btnCreateFile_Click(object sender, EventArgs e)
        {
            object i_id = this.gvYINHANGPLDLHTL1.GetRowValues(this.gvYINHANGPLDLHTL1.FocusedRowIndex, "I_ID");
            if (i_id == null)
            {
                PageTools.AlertMsg(this, "请选择要生成的文件！");
                return;
            }
            string rsNo = "-1";
            string rsMsg = "";
            this._spHelper.P_YS_YINHANGPLDLWJJK(i_id.ToString(), 1, out rsNo, out rsMsg);
            string wenjianm = this.gvYINHANGPLDLHTL1.GetRowValues(this.gvYINHANGPLDLHTL1.FocusedRowIndex, "S_WENJIANM").ToString();
            if (wenjianm.Contains("jjjq"))
            {
                wenjianm = "jjjq" + DateTime.Now.ToString("yyyyMMdd");
            }
            wenjianm = wenjianm + ".txt";
            //取消成功,重新加载列表
            if (rsNo == "1")
            {
                LoadDataYINHANGPLDLHTL1(true);
                LoadDataYINHANGPLDLHTL2(true);
                ScriptManager.RegisterStartupScript(this.updatePanel1, this.Page.GetType(), "", "DowanLoadFile('" + i_id.ToString() + "','" + wenjianm + "');", true);
            }
            else if (!string.IsNullOrEmpty(rsMsg)) PageTools.AlertMsg(this, rsMsg);
        }
 

        /// <summary>
        /// 取消文件
        /// </summary>
        protected void btnCancelFile_Click(object sender, EventArgs e)
        {
            object i_id = this.gvYINHANGPLDLHTL2.GetRowValues(this.gvYINHANGPLDLHTL2.FocusedRowIndex, "I_ID");
            if (i_id == null)
            {
                PageTools.AlertMsg(this, "请选择要取消的文件！");
                return;
            }
            string rsNo = "-1";
            string rsMsg = "";
            this._spHelper.P_YS_YINHANGPLDLWJJK(i_id.ToString(), -1, out rsNo, out rsMsg);
            //取消成功,重新加载列表
            if (rsNo == "1")
            {
                LoadDataYINHANGPLDLHTL1(true);
                LoadDataYINHANGPLDLHTL2(true);
            }
        }

        /// <summary>
        /// 注册回发 用于导出
        /// </summary>
        protected void btnCreateFile_Init(object sender, EventArgs e)
        {
            this.ScriptManager1.RegisterPostBackControl((Control)sender);
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            //加载当前水量年月
            this.txtconYM.Text = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
        }

        /// <summary>
        /// 查询付款人合同号列表
        /// </summary>
        /// <param name="reGet">是否重新读取数据库</param>
        private void LoadDataFUKUANRHTH(bool reGet)
        {
            //加载付款人合同号列表
            if (reGet)
            {
                this.dtSourceFUKUANRHTH = _spHelper.P_YS_GETFUKUANRHTHLIST(int.Parse(this.txtconYM.Text)).Tables[0];
            }
            this.gvFUKUANRHTH.DataSource = this.dtSourceFUKUANRHTH;
            this.gvFUKUANRHTH.DataBind();
            if (this.dtSourceFUKUANRHTH.Rows.Count > 0)
            {
                this.gvFUKUANRHTH.FocusedRowIndex = 0;
            }
            this.lblRecordCount.Text = "共有" + this.dtSourceFUKUANRHTH.Rows.Count.ToString() + "条记录";
        }

        /// <summary>
        /// 查询银行批量代理汇总1
        /// </summary>
        /// <param name="reGet">是否重新读取数据库</param>
        private void LoadDataYINHANGPLDLHTL1(bool reGet)
        {
            if (reGet)
            {
                this.dtSourceYINHANGPLDLHZ1 = _spHelper.P_YS_GETYINHANGPLDLHZ(0).Tables[0];
            }
            this.gvYINHANGPLDLHTL1.DataSource = this.dtSourceYINHANGPLDLHZ1;
            this.gvYINHANGPLDLHTL1.DataBind();
            if (reGet && this.dtSourceYINHANGPLDLHZ1.Rows.Count > 0)
            {
                this.gvYINHANGPLDLHTL1.FocusedRowIndex = 0;
            }
        }

        /// <summary>
        /// 查询银行批量代理汇总2
        /// </summary>
        /// <param name="reGet">是否重新读取数据库</param>
        private void LoadDataYINHANGPLDLHTL2(bool reGet)
        {
            if (reGet)
            {
                this.dtSourceYINHANGPLDLHZ2 = _spHelper.P_YS_GETYINHANGPLDLHZ(1).Tables[0];
            }
            this.gvYINHANGPLDLHTL2.DataSource = this.dtSourceYINHANGPLDLHZ2;
            this.gvYINHANGPLDLHTL2.DataBind();
        }

        #endregion

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadDataYINHANGPLDLHTL1(true);
            LoadDataYINHANGPLDLHTL2(true);
        }
        protected void btnExport_Click(object sender, EventArgs e)
        {

            //this.ASPxGridViewExporter1.GridViewID = "gvFUKUANRHTH";
            //this.gvFUKUANRHTH.DataSource = this.dtSourceFUKUANRHTH;
            //this.gvFUKUANRHTH.DataBind();
            string fileName = "HTH" + txtconYM.Text + ".xls";
            //ASPxGridViewExporter1.WriteXlsToResponse(sName);
            DataTable dt = this.dtSourceFUKUANRHTH;
           // PageTools.CreateExcel2(dt, fileName);



            HttpResponse resp = HttpContext.Current.Response;
            resp.ContentType = "application/ms-excel";
            resp.Charset = "UTF-8";
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8).ToString());
            StringBuilder str = new StringBuilder();
            str.Append("<table border=\"1px\" style=\"font-size:10pt\">");
            str.Append("<tr><td style='height:30px;' colspan='11'>收款人户名：上海市计划用水办公室预算外收入专户</td></tr>");
            str.Append("<tr><td style='height:30px;' colspan='11'>收款人账号：03349000886000236</td></tr>");
            str.Append("<tr><td style='height:30px;' colspan='11'>收款人开户银行：农行虹口区大连支行</td></tr>");
            str.Append("<tr><td style='height:30px;' colspan='11'>收款人开户银行支付系统行号：103290015051</td></tr>");
            str.Append("<tr><td style='height:30px;' colspan='11'>付款人开户银行行别：</td></tr>");
            str.Append("<tr>");//colspan
            str.Append("<td rowspan='2' style='text-align:center;font-weight:bold;height:30px;'>序号</td>");
            str.Append("<td colspan='2' style='text-align:center;font-weight:bold;height:30px;'>付款人帐号</td>");
            str.Append("<td colspan='2' style='text-align:center;font-weight:bold;height:30px;'>付款人名称</td>");
            str.Append("<td colspan='2' style='text-align:center;font-weight:bold;height:30px;'>付款人开户银行</td>");
            str.Append("<td style='text-align:center;font-weight:bold;height:30px;'>（收款行填写）</td>");
            str.Append("<td colspan='3' style='text-align:center;font-weight:bold;height:30px;'>（付款行填写）</td>");
            str.Append("</tr>");
            str.Append("<tr>");
            str.Append("<td style='text-align:center;font-weight:bold;height:30px;'>收款行填写</td>");
            str.Append("<td style='text-align:center;font-weight:bold;height:30px;'>付款行修改</td>");
            str.Append("<td style='text-align:center;font-weight:bold;height:30px;width:200px;'>收款行填写</td>");
            str.Append("<td style='text-align:center;font-weight:bold;height:30px;'>付款行修改</td>");
            str.Append("<td style='text-align:center;font-weight:bold;height:30px;width:200px;'>收款行填写</td>");
            str.Append("<td style='text-align:center;font-weight:bold;height:30px;'>付款行修改</td>");
            str.Append("<td style='text-align:center;font-weight:bold;height:30px;'>合同号码</td>");
            str.Append("<td style='text-align:center;font-weight:bold;height:30px;'>确认栏（y/n）</td>");
            str.Append("<td style='text-align:center;font-weight:bold;height:30px;'>支付系统行号</td>");
            str.Append("<td style='text-align:center;font-weight:bold;height:30px;'>备注</td>");
            str.Append("</tr>");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                str.Append("<tr>");
                str.Append("<td style='height:30px;'>").Append(dt.Rows[i]["RN"]).Append("</td>");
                str.Append("<td style=\"vnd.ms-excel.numberformat:@;\">").Append(dt.Rows[i]["S_ZHANGHAO"]).Append("</td>");
                str.Append("<td style='height:30px;'>").Append("").Append("</td>");
                str.Append("<td style='height:30px;'>").Append(dt.Rows[i]["S_HUMING"]).Append("</td>");
                str.Append("<td style='height:30px;'>").Append("").Append("</td>");
                str.Append("<td style='height:30px;'>").Append(dt.Rows[i]["S_KAIHUH"]).Append("</td>");
                str.Append("<td style='height:30px;'>").Append("").Append("</td>");
                str.Append("<td style=\"vnd.ms-excel.numberformat:@;\">").Append(dt.Rows[i]["S_HETONGH"]).Append("</td>");
                str.Append("<td style='height:30px;'>").Append("").Append("</td>");
                str.Append("<td style='height:30px;'>").Append(dt.Rows[i]["S_ZHIFUHH"]).Append("</td>");
                str.Append("<td style='height:30px;'>").Append(dt.Rows[i]["S_BEIZHU"]).Append("</td>");
                str.Append("</tr>");
            }


            str.Append("</table>");
            resp.Write(str.ToString());
            resp.End();
        }

    }
}